﻿using SqlSugar;
using TestminiApi.dto;
using TestminiApi.Extend;
using TestminiApi.model;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSwaggerGen();
builder.Services.AddEndpointsApiExplorer();
//添加跨域
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(policy =>
    {
        policy.WithOrigins("http://localhost:5173");
    });
    options.AddPolicy("cors1", policy =>
    {
        policy.WithOrigins("http://localhost:5173").AllowAnyHeader().AllowAnyMethod();
    });
});

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();

SqlSugarClient _db = new SqlSugarClient(new ConnectionConfig()
{
    ConnectionString = "server=.;database=CommonBackend;uid=sa;pwd=12345678",
    DbType = DbType.SqlServer,
    IsAutoCloseConnection = true
},
db =>
{
    db.Aop.OnLogExecuting = (sql, pars) =>
    {
        //获取原生SQL推荐 5.1.4.63  性能OK
        Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));

        //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
        //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))
    };

    //注意多租户 有几个设置几个
    //db.GetConnection(i).Aop
});

app.MapGet("api/home/getTableData", () =>
{
    return new
    {
        code = "200",
        data = new
        {
            tableData = new[]
            {
                    new
                    {
                        name = "oppo",
                        todayBuy = 500,
                        monthBuy = 3500,
                        totalBuy = 22000,
                    },
                            new
        {
            name = "vivo",
            todayBuy = 300,
            monthBuy = 2200,
            totalBuy = 24000,
        },
        new
        {
            name = "苹果",
            todayBuy = 800,
            monthBuy = 4500,
            totalBuy = 65000,
        },
        new
        {
            name = "小米",
            todayBuy = 1200,
            monthBuy = 6500,
            totalBuy = 45000,
        },
        new
        {
            name = "三星",
            todayBuy = 300,
            monthBuy = 2000,
            totalBuy = 34000,
        },
        new
        {
            name = "魅族",
            todayBuy = 350,
            monthBuy = 3000,
            totalBuy = 22000,
        },
            }
        }
    };
});
app.MapGet("api/home/getCountData", () =>
{
    return new
    {
        code = "200",
        data = new
        {
            countData = new[]
            {
               new {
                name= "今日支付订单",
                value= 1234,
                icon= "SuccessFilled",
                color= "#2ec7c9",
                },
               new {
                name= "今日收藏订单",
                    value= 210,
                icon= "StarFilled",
                color= "#ffb980",
                },
               new {
                name= "今日未支付订单",
                    value= 1234,
                icon= "GoodsFilled",
                color= "#5ab1ef",
                },
               new {
                name= "本月支付订单",
                    value= 1234,
                icon= "SuccessFilled",
                color= "#2ec7c9",
                },
               new {
                name= "本月收藏订单",
                    value= 210,
                icon= "StarFilled",
                color= "#ffb980",
                },
               new {
                name= "本月未支付订单",
                    value= 1234,
                icon= "GoodsFilled",
                color= "#5ab1ef",
                },
            }
        }
    };
});
app.MapGet("api/home/getChartData", () =>
{
    return new
    {
        code = "200",
        data = new
        {
            orderData = new
            {
                date = new[] { "20191001", "20191002", "20191003", "20191004", "20191005", "20191006", "20191007" },
                data = new[]
                {
                    new
                    {
                        苹果 =3559,
                        小米=3541,
                        华为=2409,
                        oppo=1123,
                        vivo=2312,
                        三星=6773,
                    },
                    new
                    {
                        苹果 =3559,
                        小米=3541,
                        华为=2509,
                        oppo=1623,
                        vivo=2212,
                        三星=6273,
                    },
                    new
                    {
                        苹果 =3559,
                        小米=3521,
                        华为=2109,
                        oppo=1133,
                        vivo=1212,
                        三星=6763,
                    },
                    new
                    {
                        苹果 =1559,
                        小米=2541,
                        华为=3409,
                        oppo=4123,
                        vivo=5312,
                        三星=2773,
                    },
                    new
                    {
                        苹果 =1559,
                        小米=3541,
                        华为=2409,
                        oppo=1123,
                        vivo=2312,
                        三星=6773,
                    },
                    new
                    {
                        苹果 =3559,
                        小米=1541,
                        华为=2409,
                        oppo=1123,
                        vivo=2312,
                        三星=6773,
                    },
                    new
                    {
                        苹果 =3559,
                        小米=3541,
                        华为=2409,
                        oppo=1123,
                        vivo=2312,
                        三星=6773,
                    },
                }
            },
            videoData = new[]
            {
                new
                {
                    name="小米",
                    value=2999
                },
                new
                {
                    name="苹果",
                    value=3999
                },
                new
                {
                    name="vivo",
                    value=4999
                },
                new
                {
                    name="oppo",
                    value=5999
                },
                new
                {
                    name="魅族",
                    value=6999
                },
                new
                {
                    name="三星",
                    value=7999
                },
            },
            userData = new[] {
                new
                {
                    data="周一",
                    @new=5,
                    active=200
                },
                new
                {
                    data="周二",
                    @new=10,
                    active=500
                },
                new
                {
                    data="周三",
                    @new=12,
                    active=550
                },
                new
                {
                    data="周四",
                    @new=60,
                    active=800
                },
                new
                {
                    data="周五",
                    @new=65,
                    active=550
                },
            }
        }
    };
});
app.MapPost("api/home/codeFirst", () =>
{
    _db.DbMaintenance.CreateDatabase();
    _db.CodeFirst.InitTables(typeof(UserEntiry));
});
app.MapPost("api/user/AddUser", async (UserDto par) =>
{
    var obj = par.MapTo<UserEntiry>();
    return new
    {
        code = 200,
        data = await _db.Insertable(obj).ExecuteCommandAsync()
    };
});
app.MapPost("api/user/UpdateUser", async (UserUpdateDto par) =>
{
    var obj = par.MapTo<UserEntiry>();
    return new
    {
        code = 200,
        data = await _db.Updateable(obj).ExecuteCommandAsync(),
    };
});
app.MapDelete("api/user/DeleteUser", async (int id) =>
{
    return new
    {
        code = 200,
        data = await _db.Deleteable<UserEntiry>().Where(x => x.Id == id).ExecuteCommandAsync()
    };
});
app.MapGet("api/user/GetUserList", async (string? keyword, int pageIndex = 1, int pageSize = 20) =>
{
    RefAsync<int> totalCount = 0;
    var result = await _db.Queryable<UserEntiry>().WhereIF(!string.IsNullOrEmpty(keyword), x => x.Name.Contains(keyword)).ToPageListAsync(pageIndex, pageSize, totalCount);
    return new
    {
        code = 200,
        data = new
        {
            list = result,
            totalCount = totalCount.Value,
            currentPage = totalCount % pageIndex + 1,
        }
    };
});

app.UseCors("cors1");
app.Run("http://localhost:5050");